'use strict'

let templateConfig = require('../../config/templateConfig')
let itemComponent = require('../component/itemComponent')
let utils = require('../utils/utils')
let MonsterComponent = require('../component/monsterComponent')
let totalConfig = templateConfig.totalConfig
let moneyAndExpConfig = templateConfig.other[290002]
let equipmentAttriConfig = {
  51: templateConfig.other[290003],
  52: templateConfig.other[290004],
  53: templateConfig.other[290005],
  54: templateConfig.other[290006],
  55: templateConfig.other[290007],
  60: templateConfig.other[290010],
  61: templateConfig.other[290011],
  62: templateConfig.other[290012],
  63: templateConfig.other[290013],
  64: templateConfig.other[290014],
  65: templateConfig.other[290015],
}

let addAttriCongfig = {
  str: templateConfig.other[290019].str,
  con: templateConfig.other[290019].con,
  agile: templateConfig.other[290019].agile,
  wisdom: templateConfig.other[290019].wisdom,
  attack: templateConfig.other[290019].attack,
  defend: templateConfig.other[290019].defend,
  maxHp: templateConfig.other[290019].maxHp,
  critHurt: templateConfig.other[290019].critHurt,
  critRate: templateConfig.other[290019].critRate,
  skillRate: templateConfig.other[290019].skillRate,
  skillHurtPer: templateConfig.other[290019].skillHurtPer,
}
let weaponTypes = [51, 52, 53, 54, 55] //掉落武器池
let equipmentTypes = [60, 61, 62, 63, 64, 65] //副本怪物掉落装备池
let prefixAttriArray1 = templateConfig.prefixAttri[410000][1] //前缀属性
let prefixAttriArray2 = templateConfig.prefixAttri[410000][2] //前缀属性
let prefixAttriConfig = templateConfig.prefixAttri
let monsterSkills = [450001, 450010, 450020, 450030, 450050, 450040, 450060]

class CreateMonsterHandle {
  constructor() {}

  //创建普通怪物
  createNormalMonster(lvl, monsterName) {
    let monster = new MonsterComponent({
      lvl: lvl,
      name: monsterName || '怪物'
    })
    this.addPrefixAttri(monster) //添加前缀
    this.addSkill(monster)
    monster.fightAttri.hp = monster.fightAttri.maxHp
    return monster
  }

  //创建副本,洞穴怪物
  createCaveMonster(monsterId) {
    let monsterConfig = totalConfig[monsterId]
    let monster = new MonsterComponent(monsterConfig)
    this.addPrefixAttri(monster) //添加前缀
    monster.fightAttri.hp = monster.fightAttri.maxHp
    return monster
  }

  //创建魔窟怪物
  createDevilCaveMonster(monsterId) {
    let monsterConfig = totalConfig[monsterId]
    let monster = new MonsterComponent(monsterConfig)
    return monster
  }

  //创建野外boss
  createFieldBoss(fieldBossConfig) {
    let monster = new MonsterComponent(fieldBossConfig)
    return monster
  }

  //创建强盗
  createRobber(lvl) {
    let monster = new MonsterComponent({
      lvl: lvl,
      name: '强盗'
    })
    return monster
  }

  //创建机器人
  createRobot(lvl, monsterId) {
    let monster
    if (!monsterId) {
      monster = new MonsterComponent({
        lvl: lvl,
        name: '机器人'
      })
    } else {
      let monsterConfig = templateConfig.monster[monsterId]
      monster = new MonsterComponent(monsterConfig)

    }
    monster.type = 1 //'机器人类型'
    return monster
  }

  //创建怪物掉落的材料道具
  createDropProp(props) {
    return utils.getOneFromArray(props)
  }

  //创建装备属性 flag=1无魂器普通怪  2有魂器副本怪 3有魂器魔窟怪
  createEquipment(lvl, flag, secretMapId, lucky) {
    let mapConfig = totalConfig[secretMapId]
    let equipmentType, equipmentRate
    let specialAttri = {}
      //先判断装备的掉率
    if (flag === 1) {
      if (Math.random() * 100 < mapConfig.equipmentRate1[0]) {
        return null
      }
      equipmentRate = mapConfig.equipmentRate1.slice(1)
    } else if (flag === 2) {
      if (Math.random() * 100 < mapConfig.equipmentRate2[0]) {
        return null
      }
      equipmentRate = mapConfig.equipmentRate2.slice(1)
    } else if (flag === 3) {
      if (Math.random() * 100 < mapConfig.equipmentRate3[0]) {
        return null
      }
      equipmentRate = mapConfig.equipmentRate3.slice(1)
    } else {
      return null
    }

    //判断出什么装备
    if (Math.random() * 100 < 20) { //几率出武器
      equipmentType = utils.getOneFromArray(weaponTypes)
      let attriConfig = equipmentAttriConfig[equipmentType].specialAttri
        //创建武器的特殊属性
      for (let i in attriConfig) {
        specialAttri[i] = utils.addRandomHalf2(attriConfig[i][0] * lvl + attriConfig[i][1])
      }
    } else { //几率出防具,魂器,戒指
      equipmentType = utils.getOneFromArray(equipmentTypes)
    }

    //创建装备主属性
    let mainAttri = {}
    let mainAttriConfig = equipmentAttriConfig[equipmentType].mainAttri
    for (let i in mainAttriConfig) {
      mainAttri[i] = utils.addRandomHalf2(mainAttriConfig[i][0] * lvl + mainAttriConfig[i][1])
    }
    //创建装备附加属性
    let addAttri = {}
    let addAttriId = equipmentAttriConfig[equipmentType].addAttriId
    let addAttriPool = totalConfig[addAttriId].attris
      //根据幸运值获得装备品质
    let quality = utils.getIndexByLucky(lucky, mapConfig.luckyRate, equipmentRate)
    let array = utils.getRandomArray(quality, addAttriPool)
    for (let i in array) {
      let attriConfig = addAttriCongfig[array[i]]
      addAttri[array[i]] = utils.addRandomHalf2(attriConfig[0] * lvl + attriConfig[1])
    }
    let equipment = itemComponent.getPropComponent({
      templateId: equipmentType * 10000,
      lvl: lvl,
      quality: quality + 1,
      mainAttri: mainAttri,
      addAttri: addAttri,
      specialAttri: specialAttri,
    })

    return equipment
  }

  //创建装备属性 flag=1无魂器普通怪  2有魂器副本怪 3有魂器魔窟怪
  test_createEquipment(type, lvl, flag, secretMapId, lucky) {
    let mapConfig = totalConfig[secretMapId]
    let equipmentType, equipmentRate
    let specialAttri = {}
      //先判断装备的掉率

      if (flag === 1) {
        equipmentRate = mapConfig.equipmentRate1.slice(1)
      } else if (flag === 2) {
        equipmentRate = mapConfig.equipmentRate2.slice(1)
      } else if (flag === 3) {
        equipmentRate = mapConfig.equipmentRate3.slice(1)
      } else {
        return null
      }

    equipmentType = type

    if (equipmentType < 60) { //出武器
      equipmentType = utils.getOneFromArray(weaponTypes)
      let attriConfig = equipmentAttriConfig[equipmentType].specialAttri
        //创建武器的特殊属性
      for (let i in attriConfig) {
        specialAttri[i] = utils.addRandomHalf2(attriConfig[i][0] * lvl + attriConfig[i][1])
      }
    }
    //创建装备主属性
    let mainAttri = {}
    let mainAttriConfig = equipmentAttriConfig[equipmentType].mainAttri
    for (let i in mainAttriConfig) {
      mainAttri[i] = utils.addRandomHalf2(mainAttriConfig[i][0] * lvl + mainAttriConfig[i][1])
    }
    //创建装备附加属性
    let addAttri = {}
    let addAttriId = equipmentAttriConfig[equipmentType].addAttriId
    let addAttriPool = totalConfig[addAttriId].attris
      //根据幸运值获得装备品质
    let quality = utils.getIndexByLucky(lucky, mapConfig.luckyRate, equipmentRate)
    let array = utils.getRandomArray(quality, addAttriPool)
    for (let i in array) {
      let attriConfig = addAttriCongfig[array[i]]
      addAttri[array[i]] = utils.addRandomHalf2(attriConfig[0] * lvl + attriConfig[1])
    }
    let equipment = itemComponent.getPropComponent({
      templateId: equipmentType * 10000,
      lvl: lvl,
      quality: quality + 1,
      mainAttri: mainAttri,
      addAttri: addAttri,
      specialAttri: specialAttri,
    })

    return equipment
  }

  //添加前缀属性
  addPrefixAttri(monster) {
    let lvl = monster.fightAttri.lvl
    let prefixAttriArray = prefixAttriArray1
    if (Math.floor(Math.random() * 100) < (lvl - 1) * 20) {
      prefixAttriArray = prefixAttriArray2
    }
    let prefixAttriId = utils.getOneFromArray(prefixAttriArray)
    let attri = prefixAttriConfig[prefixAttriId].attri
    monster.fightAttri.prefixAttriId = prefixAttriId
    for (let i in attri) {
      monster.fightAttri[i] += attri[i][0] * lvl + attri[i][1]
    }
  }


  addSkill(monster) {
    if (Math.floor(Math.random() * 100) < 70) {
      let skillId = utils.getOneFromArray(monsterSkills)
      monster.fightAttri.attackSkill = [skillId]
      monster.fightAttri.skillRate += 30
      monster.fightAttri.skills = [skillId]
    }
  }
}

let createMonsterHandle = new CreateMonsterHandle()
module.exports = createMonsterHandle
